package krui.weibo.ui;

import java.net.MalformedURLException;
import java.util.List;

import krui.itcast.db.DataHelper;
import krui.itcast.db.UserInfo;
import krui.itcast.db.WeiboInfo;
import krui.weibo.util.AsyncGetCommentsLoader;
import krui.weibo.util.AsyncGetMentionsLoader;
import krui.weibo.util.AsyncGetWeiboLoader;
import krui.weibo.util.AsyncNetCheckLoader;
import krui.weibo.util.DownLoadEmotion2local;
import krui.weibo.util.ImgProcess;
import krui.weibo.util.WeiboHelper;
import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.WeiboException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FrinedsTimeline extends Activity {
	Status status;
	String url;
	Bitmap proBitmap;
	String userName;
	List<WeiboInfo> wbList;
	Boolean haveImage = false;
	Boolean haveVer = false;
	final int MENU_ACCOUNT = Menu.FIRST;
	final int MENU_SEARCH = Menu.FIRST+1;
	final int MENU_SET = Menu.FIRST+2;
	final int MENU_ABOUT = Menu.FIRST+3;
	final int MENU_EXIT = Menu.FIRST+4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.home);
		
		//检查网络
		new AsyncNetCheckLoader(FrinedsTimeline.this).execute();
		Weibo weibo = WeiboHelper.getInstance().getWeibo();
		UserInfo user = WeiboHelper.getInstance().getUser();
		if (user.getUserName() == null || user.getUserIcon() == null) {
			try {
				DataHelper dbHelper = new DataHelper(FrinedsTimeline.this);
				List<UserInfo> userList = dbHelper.GetUserList(true);
				user = userList.get(0);
				// 获取用户名
				userName = weibo.showUser(user.getUserId()).getName();
				// 获取头像地址
				url = weibo.showUser(user.getUserId()).getProfileImageURL()
						.toString();
				// 下载头像
				try {
					proBitmap = ImgProcess.DownloadImg(url);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// 将获取到的用户名跟头像存入数据库
				dbHelper.UpdateUserInfo(userName, proBitmap, user.getUserId());
				userList = dbHelper.GetUserList(false);
				user = userList.get(0);
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			userName = user.getUserName();
			proBitmap = user.getUserIcon();
		}
		// 在窗口显示用户名
		TextView textView = (TextView) findViewById(R.id.showname);
		textView.setText(userName + "'s Weblog");
		// 在窗口显示登录用户头像
		ImageView iv = (ImageView) findViewById(R.id.proimg);
		iv.setImageBitmap(proBitmap);

		// 异步刷新微博
		ListView msgList = (ListView) findViewById(R.id.Msglist);
		LinearLayout loadinglayout = (LinearLayout) findViewById(R.id.loadingLayout);
		new AsyncGetWeiboLoader(msgList, loadinglayout, this).execute();

		// 主时间线按钮
		ImageButton homeBtn = (ImageButton) findViewById(R.id.homeBtn);
		homeBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ListView msgList = (ListView) findViewById(R.id.Msglist);
				LinearLayout loadinglayout = (LinearLayout) findViewById(R.id.loadingLayout);
				new AsyncGetWeiboLoader(msgList, loadinglayout,
						FrinedsTimeline.this).execute();
				Toast.makeText(FrinedsTimeline.this, "数据加载中，请稍后...",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 提到当前用户按钮
		ImageButton atBtn = (ImageButton) findViewById(R.id.atBtn);
		atBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ListView msgList = (ListView) findViewById(R.id.Msglist);
				LinearLayout loadinglayout = (LinearLayout) findViewById(R.id.loadingLayout);
				loadinglayout.setVisibility(View.VISIBLE);
				new AsyncGetMentionsLoader(msgList, loadinglayout,
						FrinedsTimeline.this).execute();
				Toast.makeText(FrinedsTimeline.this, "数据加载中，请稍后...",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 获取评论列表按钮
		ImageButton commentBtn = (ImageButton) findViewById(R.id.chatBtn);
		commentBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ListView msgList = (ListView) findViewById(R.id.Msglist);
				LinearLayout loadinglayout = (LinearLayout) findViewById(R.id.loadingLayout);
				loadinglayout.setVisibility(View.VISIBLE);
				new AsyncGetCommentsLoader(msgList, loadinglayout,
						FrinedsTimeline.this).execute();
				Toast.makeText(FrinedsTimeline.this, "数据加载中，请稍后...",
						Toast.LENGTH_SHORT).show();
			}
		});
		

		// 刷新按钮
		ImageButton refreshBtn = (ImageButton) findViewById(R.id.refreshBtn);
		refreshBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ListView msgList = (ListView) findViewById(R.id.Msglist);
				LinearLayout loadinglayout = (LinearLayout) findViewById(R.id.loadingLayout);
				loadinglayout.setVisibility(View.VISIBLE);

				new AsyncGetWeiboLoader(msgList, loadinglayout,
						FrinedsTimeline.this).execute();

				Toast.makeText(FrinedsTimeline.this, "数据加载中，请稍后...",
						Toast.LENGTH_SHORT).show();
			}
		});
		// 发布微博按钮
		ImageButton writeBtn = (ImageButton) findViewById(R.id.writeBtn);
		writeBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent it = new Intent(FrinedsTimeline.this, UpdateStatus.class);
				Bundle bundle = new Bundle();
				String stateString = "update";
				bundle.putString("state", stateString);
				bundle.putBoolean("isUpdate", true);
				it.putExtras(bundle);
				startActivity(it);
			}
		});
	}

	// 自带返回键操作
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 弹出确定退出对话框
			new AlertDialog.Builder(this)
					.setTitle("退出")
					.setMessage("确定退出吗？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									Intent exit = new Intent(Intent.ACTION_MAIN);
									exit.addCategory(Intent.CATEGORY_HOME);
									exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									startActivity(exit);
									System.exit(0);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.cancel();
								}
							}).show();
			// 这里不需要执行父类的点击事件，所以直接return
			return true;
		}
		// 继续执行父类的其他点击事件
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_ACCOUNT, 0, "账号").setIcon(R.drawable.switchuser);
		menu.add(0, MENU_SEARCH, 1, "搜索").setIcon(R.drawable.search);
		menu.add(0, MENU_SET, 3, "设置").setIcon(R.drawable.menu_switchuser_setting);
		menu.add(0, MENU_ABOUT, 4, "关于").setIcon(R.drawable.menu_switchuser_about);
		menu.add(0, MENU_EXIT, 5, "退出").setIcon(R.drawable.menu_exit);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case MENU_ACCOUNT:
			Toast.makeText(FrinedsTimeline.this, "账户", Toast.LENGTH_SHORT).show();
			break;
		case MENU_SEARCH:
			Toast.makeText(FrinedsTimeline.this, "搜索", Toast.LENGTH_SHORT).show();
			break;
		case MENU_SET:
			Toast.makeText(FrinedsTimeline.this, "设置", Toast.LENGTH_SHORT).show();
			break;
		case MENU_ABOUT:
			Toast.makeText(FrinedsTimeline.this, "关于", Toast.LENGTH_SHORT).show();
			break;
		case MENU_EXIT:
			new AlertDialog.Builder(this)
			.setTitle("退出")
			.setMessage("确定退出吗？")
			.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							// TODO Auto-generated method stub
							Intent exit = new Intent(Intent.ACTION_MAIN);
							exit.addCategory(Intent.CATEGORY_HOME);
							exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(exit);
							System.exit(0);
						}
					})
			.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
					}).show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	
	

}
